package com.qiyu.xujia.demo;



import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.util.IOUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.*;
import org.springframework.http.client.*;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;


public class DemoRequest {



    public static void doPost2(String url) throws URISyntaxException, IOException {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        ClientHttpRequest request = factory.createRequest(new URI(url), HttpMethod.POST);
        ClientHttpResponse httpResponse = request.execute();
        InputStream stream = httpResponse.getBody();
        byte[] bytes = FileCopyUtils.copyToByteArray(stream);
        JSONObject jsonObject = JSON.parseObject(bytes);
        System.out.println(jsonObject);

    }

    public static void doPost3(String url) throws URISyntaxException, IOException {
        OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory();
        ClientHttpRequest request = factory.createRequest(new URI(url), HttpMethod.POST);
        ClientHttpResponse httpResponse = request.execute();
        InputStream stream = httpResponse.getBody();
        byte[] bytes = FileCopyUtils.copyToByteArray(stream);
        JSONObject jsonObject = JSON.parseObject(bytes);
        System.out.println(jsonObject);
    }


    /**
     * form表单请求
     * @param url
     * @param mapData
     */
    public static void doPost1(String url, Map<String,Object> mapData) {
        RestTemplate template = new RestTemplate();
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        for(Map.Entry<String, Object> m : mapData.entrySet()){
            map.add(m.getKey(), m.getValue());
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(map, headers);
        JSONObject jsonObject = template.execute(url, HttpMethod.POST, template.httpEntityCallback(entity), new ResponseExtractor<JSONObject>() {
            @Override
            public JSONObject extractData(@NotNull ClientHttpResponse response) throws IOException {
                InputStream stream = response.getBody();
                byte[] bytes = FileCopyUtils.copyToByteArray(stream);
                return JSON.parseObject(bytes);
            }
        });
        System.out.println(jsonObject);
    }

    public static void countJson() throws IOException {
        JSONObject jsonObject = JSONObject.parseObject(new String(Files.readAllBytes(Paths.get("d:/test.json"))));
        Set<String> keys = jsonObject.keySet();
        System.out.println(keys.size());
    }

    public static void main(String[] args) throws  IOException {
//        String url="http://118.145.151.16/etlctl/reportdata/?interface_code=ads_xl_caft_clue_mon_keydata";
//        HashMap<String, Object> mapData = new HashMap<>();
//        mapData.put("period_date","2024-06-07");
//        mapData.put("operate_type","2");
//        mapData.put("export_type","2");
//        doPost1(url,mapData);
        countJson();
    }
}
